package com.jswlw.controller.admin.base;

import com.jswlw.controller.BaseController;
import com.jswlw.entity.base.User;
import com.jswlw.service.base.RoleService;
import com.jswlw.service.base.UserService;
import com.jswlw.web.manager.SessionAdminManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统用户管理
 * Created by wu on 2017-12-07.
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController extends BaseController {

    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    /**
     * 主页
     *
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(Model model) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/index");
        return modelAndView;
    }

    /**
     * 用户列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        Map condition = new HashMap<>();
        condition.putAll(parseParams(request));
        //总条数
        resultMap.put("total", userService.findCount(condition));
        //获取每页数据
        resultMap.put("rows", userService.findAll(condition));
        return resultMap;
    }

    /**
     * 新增用户
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles", roleService.findAll(new HashMap()));
        modelAndView.setViewName("user/edit");
        return modelAndView;
    }

    /**
     * 编辑用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles", roleService.findAll(new HashMap()));
        modelAndView.addObject("info", userService.findOne(id));
        modelAndView.setViewName("user/edit");
        return modelAndView;
    }

    /**
     * 保存用户
     *
     * @param model
     * @param adminUser
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(Model model, User adminUser) {
        try {
            if (adminUser.getId() == null) {
                userService.insert(adminUser);
            } else {
                userService.update(adminUser);
            }
            return this.successResult("保存成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    /**
     * 删除用户
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ModelMap delete(Model model, Integer id) {
        try {
            userService.delete(id);
            return this.successResult("删除成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("删除失败" + e.getMessage());
        }
    }

    /**
     * 设置角色
     *
     * @param id
     * @return
     */
    @Deprecated
    @RequestMapping("/auth")
    public ModelAndView auth(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("menus", roleService.findAll(new HashMap()));
        modelAndView.addObject("roleMenus", userService.findUserRole(id));
        modelAndView.addObject("id", id);
        modelAndView.setViewName("user/auth");
        return modelAndView;
    }

    /**
     * 保存角色
     *
     * @param model
     * @param roles
     * @param id
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/save_auth", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap saveAuth(Model model, Integer[] roles, Integer id) {
        try {
            userService.saveUserAuth(roles, id);
            return this.successResult("保存成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    @RequestMapping(value = "/changeUserInfo",method = RequestMethod.GET)
    public ModelAndView changeUserInfo() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/changeUserInfo");
        modelAndView.addObject("userInfo", SessionAdminManager.getCurrentUser());
        return modelAndView;
    }

    @RequestMapping(value = "/doChangeUserInfo",method = RequestMethod.POST)
    public ModelAndView doChangeUserInfo(@RequestBody  User user) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/changeUserInfo");
        modelAndView.addObject("userInfo", SessionAdminManager.getCurrentUser());
        return modelAndView;
    }
}
